<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:09:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceAcceptanceRepository;

/**
 * @property FinanceAcceptanceRepository $repository 
 */
class AcceptanceService extends Service
    {
    public $state = 1;
    public function boot(FinanceAcceptanceRepository $repository)
        {
        $this->repository = $repository;
        }

    public function info()
        {
        $this->repository->setRelation(['customer' => ['billtrade'], 'supplier' => ['billtrade']]);
        return $this->repository->whereId(request("id"))->getOne();
        }
    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    protected function save()
        {
        $data                   = request()->toArray();
        $data['handletrade_id'] = request('trade_id');
        return $this->repository->autosave($data);
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据已{$gdata->status->name}！不能审核！");
                }
            return $gdata->handFinace();
            });
        }
    protected function complete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 1) {
                fail("单据已{$gdata->status->name}！不能使用！");
                }
            return $gdata->useFinace();
            });
        }
    protected function debtor()
        {
        $request = request();
        $gdata   = $this->repository->whereId($request->id)->first();

        if ($gdata) {
            if ($gdata->status_id != 1) {
                fail("单据已{$gdata->status->name}！不能入账！");
                }
            $gdata = $this->repository->autosave(['id' => $request->id, 'account_id' => $request->account_id, 'usedate' => $request->usedate]);
            return $gdata->debtFinace();
            }
        else {
            fail("单据不存在！！");
            }
        }

    protected function debtFinace()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据已{$gdata->status->name}！不能审核！");
                }
            return $gdata->finance();
            });
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("单据[{$v->status->name}]！禁止删除");
                }
            return $v->delete();
            });
        }
    protected function saveSchedule()
        {
        $schedule_id = request('schedule_id');
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v) use ($schedule_id)
            {
            $v->schedule_id = $schedule_id;
            return $v->save();
            });
        }
    }